﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.DBM;
using KPIS.GERP.GBMS.DAL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DALFactory;
using KPIS.GERP.GBMS.IBLL;
using System.Globalization;

namespace KPIS.GERP.GBMS.BLL
{
    public sealed class RequestPass
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public DataTable GetData(string bgm_req_seq)
        {
            DataTable dtb;
            string strSQL;

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@bgm_req_seq", bgm_req_seq);
            strSQL = "select rp.*, j.job_site_name"
                + " from bgm_request_pass rp"
                + " left join hrm_mp_job_sites j on rp.job_site_seq = j.job_site_seq"
                + " where rp.bgm_req_seq = @bgm_req_seq";
            dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            dbManager.Dispose();
            return dtb;
        }

        public DataTable GetInfo(string bgm_req_pass_seq)
        {
            DataTable dtb;
            string strSQL;

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@bgm_req_pass_seq", bgm_req_pass_seq);
            strSQL = "select rp.*, j.job_site_name"
                + " from bgm_request_pass rp"
                + " left join hrm_mp_job_sites j on rp.job_site_seq = j.job_site_seq"
                + " where rp.bgm_req_pass_seq = @bgm_req_pass_seq";
            dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            dbManager.Dispose();
            return dtb;
        }

        public int Insert(string bgm_req_seq, string job_site_seq, string CreatedBy)
        {
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);
                dbManager.AddParameters(0, "@bgm_req_seq", bgm_req_seq);
                dbManager.AddParameters(1, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(2, "@created_by", CreatedBy);
                dbManager.AddParameters(3, "@created_when", DateTime.Now);

                string strsql = "Insert into bgm_request_pass (bgm_req_seq, job_site_seq, created_by, created_when)"
                              + " values(@bgm_req_seq, @job_site_seq, @created_by, @created_when)";
                dbManager.ExecuteNonQuery(CommandType.Text, strsql);

                string strsqlLast_insert = "select distinct scope_identity() FROM bgm_request_pass";
                DataSet ds = new DataSet();
                ds = dbManager.ExecuteDataSet(CommandType.Text, strsqlLast_insert);
                dbManager.CommitTransaction();
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            catch (Exception e)
            {
                dbManager.RollBackTransaction();
                return -1;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(string bgm_req_pass_seq, string bgm_req_seq, string job_site_seq, string UpdatedBy)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);
                dbManager.AddParameters(0, "@bgm_req_pass_seq", bgm_req_pass_seq);
                dbManager.AddParameters(1, "@bgm_req_seq", bgm_req_seq);
                dbManager.AddParameters(2, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(3, "@updated_by", UpdatedBy);
                dbManager.AddParameters(4, "@updated_when", DateTime.Now);

                string strsql = "Update bgm_request_pass"
                    + " set bgm_req_seq = @bgm_req_seq"
                    + ", job_site_seq = @job_site_seq"

                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " where bgm_req_pass_seq = @bgm_req_pass_seq";
                dbManager.ExecuteNonQuery(CommandType.Text, strsql);
            }
            catch (Exception e)
            {
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(string bgm_req_pass_seq, string UpdatedBy)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@bgm_req_pass_seq", bgm_req_pass_seq);

                string strsql = "delete from bgm_request_pass"
                    + " where bgm_req_pass_seq = @bgm_req_pass_seq";
                dbManager.ExecuteNonQuery(CommandType.Text, strsql);
            }
            catch (Exception e)
            {
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}